using System.Diagnostics;

namespace Promote {
    public class Base2 {
        public static uint LogCeil(int x) {
            Debug.Assert(x > 0);
            return LogCeil((uint)x);
        }

        public static uint LogCeil(uint x) {
            Debug.Assert(x != 0);
            uint result = 0;
            if( (x & 0xffff0000) != 0 ) { x >>= 16; result += 16; }
            if( (x & 0x0000ff00) != 0 ) { x >>= 8; result += 8; }
            if( (x & 0x000000f0) != 0 ) { x >>= 4; result += 4; }
            if( (x & 0x0000000c) != 0 ) { x >>= 2; result += 2; }
            if( (x & 0x00000002) != 0 ) { ++result; }
            return result;
        }

        public static uint LogCeil(long x) {
            Debug.Assert(x > 0);
            return LogCeil((ulong)x);
        }

        public static uint LogCeil(ulong x) {
            Debug.Assert(x != 0);
            uint result = 0;
            if( (x & 0xffffffff00000000) != 0 ) { x >>= 32; result += 32; }
            if( (x & 0x00000000ffff0000) != 0 ) { x >>= 16; result += 16; }
            if( (x & 0x000000000000ff00) != 0 ) { x >>= 8; result += 8; }
            if( (x & 0x00000000000000f0) != 0 ) { x >>= 4; result += 4; }
            if( (x & 0x000000000000000c) != 0 ) { x >>= 2; result += 2; }
            if( (x & 0x0000000000000002) != 0 ) { ++result; }
            return result;
        }

        private Base2() { }
    }
}

